class VerifyController < ApplicationController
  
  def legal_fullname
    fullname=params[:fullname]
    if fullname.length>1 and fullname.length<=20  #needs to escape special character to ensure safety
      @message="OK"
    else
      @message="Length should between 1 and 20"
    end
  end
  
  def username
    username=params[:username]
    if legal_username(username)==0
      @message="Username only accepts 1-16 chracters,numbers,underline or spaces"
    elsif unique_username(username)==0
      @message="Username is taken"
    else
      @message="OK"      
    end
    
  end
  
  def email
    email=params[:email]
    if legal_username(email)==0
      @message="Please check your email"
    elsif unique_email(email)==0
      @message="Email is Taken"
    else
      @message="OK"
    end
    
    
  end
  
  private
  
  def unique_email(email)
    if User.find_by_email(email).nil?
      return 1  #email is available
    else
      return 0  #email is unavailable
    end
  end
  
  def legal_username(email)
    if email=~/^[a-zA-Z0-9_\.]+@[a-zA-Z0-9-]+[\.a-zA-Z]+$/
      return 1  #email is legal
    else
      return 0  #email is illegal
    end
  end
  
  
  def unique_username(username)
    if User.find_by_username(username).nil?
      return 1  #username is available
    else
      return 0  #username is unavailable
    end
    
  end
  
  def legal_username(username)
    if username=~/\w{1,16}$/
      return 1  #username is legal
    else
      return 0  #username is illegal
    end
    
  end

  
end
